package com.dz.risk.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author Doke
 * @date 2021/8/17 9:04
 */
public class FutureUtils {

    /**
     * 超时时间
     */
    public static final int TIME = 30;

    /**
     * @param futures futures 任务
     * @param <T>     <T>  泛型
     * @return {@link List} 泛型T
     * @throws InterruptedException
     * @throws ExecutionException
     * @throws TimeoutException
     */
    public static <T> List<T> getValues(List<Future<T>> futures) throws InterruptedException, ExecutionException, TimeoutException {
        List<T> list = new ArrayList<>();
        for (Future<T> future : futures) {
            T t = future.get(TIME, TimeUnit.MINUTES);
            if (Objects.nonNull(t)) {
                list.add(t);
            }
        }
        return list;

    }
}
